    var discounts = {'#slider':'#discount'};
    var d;

    $(document).ready(function()
    {
        $('#slider').slider({ value: $('#discount').text() });
        $.each(discounts,function(slider,discount)
        {
            $(slider).slider({
                min: minDiscount,
                max: maxDiscount,
                stop: function(event, ui)
                {
                    $(discount).text($(slider).slider('values',0));
                },
                slide: function(event, ui)
                {
                    $(discount).text($(slider).slider('values',0));
                }
            });
        });
        $("#startDate").datepicker(
                {
                    dateFormat: 'yy-mm-dd',
                    numberOfMonths: 2,
                    minDate: new Date(),
                    onSelect: function(selected)
                    {
                        $("#endDate").datepicker("option","minDate", selected)
                    }
                });
        $("#endDate").datepicker(
                {
                    dateFormat: 'yy-mm-dd',
                    numberOfMonths: 2,
                    onSelect: function(selected)
                    {
                        $("#startDate").datepicker("option","maxDate", selected)
                    }
                });
        $('#startDate').datepicker("setDate", new Date());
        $('#endDate').datepicker("setDate", new Date());
        getPromocodes();
        $("#sortBy").change(function()
        {
            //console.log($("#sortBy").val());
            getPromocodes();
        });
        $("#showExpired").change(function()
        {
            getPromocodes();
        });
    });

    function generatePromocodes()
    {
		$('#message').stop(true,true);
        $.post('dashboard',
		{
			action: 'generatePromocodes',
			discount: $('#discount').text().trim(),
			startDate: $('#startDate').val(),
			endDate: $('#endDate').val(),
			number: $('#number').val()
		},
		function(data)
		{
			if (data == "success")
			{
				$('#message').css('color','black');
				$('#message').html("Success");
			}
			else
			{
				$('#message').css('color','red');
				$('#message').html("Failure");
			}
		});
		$('#message').fadeOut(5000);
    }

    function getPromocodes()
    {
        var div = $('#promocodes');
        div.empty();
        div.append('Obtaining promotional codes...');
        //alert('getting promocodes');
        $.post('dashboard',
                {
                    action: "getPromocodes",
                    sortBy: $('#sortBy').val(),
                    showExpired: $('#showExpired').prop('checked')
                },
                function(data)
                {
                    displayPromocodes($.parseJSON(data));
                });
    }

function displayPromocodes(data)
{
	//console.log(data);
	//alert('displaying header');
	var div = $('#result-table');
	div.empty();
	if (data.codesList.length == 0)
	{
		div.html('No promotional codes found');
	}
	else
	{
		var table = '';
		var headerRow = '<thead><tr style="background:white">';
		headerRow += '<th>#</th>';
		headerRow += '<th>Code</th>';
		headerRow += '<th>Starting date</th>';
		headerRow += '<th>Expiration date</th>';
		headerRow += '<th>Discount, %</th>';
		headerRow += '</tr></thead>';
		table += headerRow;
		table += '<tbody id="result_body">';
		$.each(data.codesList, function (index, element)
		{
			var ind = parseInt(index) + 1;
			var row = "";
			var color = index % 2 == 1 ? 'white' : '#C5FEFE';
			row += '<tr style="background:' + color + '">';
			row += '<td>' + ind + '</td>';
			row += '<td>' + element.code + '</td>';
			row += '<td>' + element.startDate + '</td>';
			row += '<td>' + element.expirationDate + '</td>';
			row += '<td>' + element.discount + '</td>';
			row += '</tr>';
			table += row;
		});
		table += '</tbody>';
		div.html(table);
		pager = new Pager('result_body', 10, 'pager', 'pageNavPosition');
		pager.init();
		pager.showPage(1);
		$('#results-count').html(data.codesList.length);
	}
}

function validate(evt) {
	var theEvent = evt || window.event;
	var key = theEvent.keyCode || theEvent.which;
	key = String.fromCharCode( key );
	var regex = /[0-9]|\./;
	if( !regex.test(key) ) 
	{
		theEvent.returnValue = false;
		if(theEvent.preventDefault) theEvent.preventDefault();
	}
}